<?php
/*
 * @Descripttion: 
 * @version: v1
 * @Author: Terry
 * @Date: 2021-10-05 11:39:33
 * @LastEditors: Terry
 * @LastEditTime: 2024-12-07 23:24:27
 */
declare (strict_types = 1);

namespace app\middleware;
use think\facade\Db;
use \Firebase\JWT\JWT;

class CheckLogin
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
      //判断网络头是否携带token
        if(isset($_SERVER["HTTP_AUTHORIZATION"])){
          $token = $_SERVER["HTTP_AUTHORIZATION"];
        }else{
          return result(TOKEN_TIMEOUT,'','token已过期');
        }
        //用户唯一码
        $uuid = $_SERVER["HTTP_UUID"] ??  '';
        //项目唯一码
        $prjCode = $_SERVER["HTTP_PUUID"] ?? '';
        //企业唯一码
        $corpCode = $_SERVER["HTTP_CORPCODE"] ?? '';

        // //判断token是否解密成功或匹配
        // if($uuid != 'test' && !$this->checkToken($token,$uuid)){
        //     return result(TOKEN_TIMEOUT,'','token已过期');
        // }
        if($uuid == 'wxapp') $uuid = '578D95309CB311EFA3A900163E0AB611';
        if($uuid == 'admin') $uuid = 'AA6EBF60607E11EBB06E00163E06534A';
        
        // 多语言
        $lang = !empty($_SERVER["HTTP_LANG"]) && $_SERVER["HTTP_LANG"] !== "null" ? $_SERVER["HTTP_LANG"]  : 'zh-cn';
        $langField = [
          'zh-cn' => 'zhCnName as name' ,
          'zh_cn' => 'zhCnName as name' ,
          'zh_mo'=> 'zhTwName as name',
          'zh_tw' => 'zhCnName as name' ,
          'zh_hk'=> 'zhTwName as name',
          'zh-tw'=> 'zhTwName as name',
          'en'=>'enName as name'
        ];
        $language = [
          'zh-cn'=>'zh_cn',
          'zh_cn'=>'zh_cn',
          'zh-tw'=>'zh_tw',
          'zh_tw'=>'zh_tw',
          'zh-hk'=>'zh_tw',
          'zh_hk'=>'zh_tw',
          'zh-mo'=>'zh_tw',
          'zh_mo'=>'zh_tw',
          'en'=>'en',
        ];
        
        //通过api接口url 判断用户的入口。为后续中间件做判断依据
        $path = $request->baseUrl();
        if (preg_match('/\/api\/certificate\/(user|admin)\//', $path, $matches)) {
            $userPortal = $matches[1]; // user 或 admin
        }

        $request->uuid =$uuid;
        $request->prjCode =$prjCode;
        $request->corpCode =$corpCode;
        $request->lang = $langField[$lang];
        $request->language = $language[$lang];
        $request->userPortal = $userPortal ?? 'admin';
        $req =  $next($request);
        //problem 1
        return $req;
    }
        /**
       * [checkToken 解密token]
       * @param  [type] $userData.token [token]
       * @param  [type] $userData.phone [手机号]
       * @return [type]           [description]
       */
      private  function checkToken($token,$uuid){
        $key = DB::connect('platform')->name('config')->value('inside_key');
        try{
            $decode = JWT::decode($token,$key,['HS256']);
            if ($decode->iss!='AUXFACE'|| $decode->uuid!=$uuid)  return false;
            return true;
        }catch (\Exception $e){
            return false;
        }
    }

}
